package com.ruoyi.plugins;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;

/**
 * 这个文件是当网页播放不了的时候由网页调起本地视频播放器来播放视频
 */
public class hsmyldkVideoPlayerStarter {
    public static void main(String[] args) throws IOException {
//        System.getProperties().forEach((k, v) -> {
//            System.out.println(k + "----" + v);
//        });
        //hsmyldkLocalPlayVideo://192.168.0.110/dev-api/profile/upload/2022/02/05/3a0d0c42-2193-4602-ad3a-a1cd81163b43.mp4
        //C:\Program Files\MPC-BE x64\mpc-be64.exe
        String rootPath = hsmyldkVideoPlayerStarter.class.getResource("").getPath();
//        File file1 = new File(rootPath + File.separator + "aaa.txt");
//        if (!file1.exists()) {
//            file1.createNewFile();
//        }
//        writeText(file1, new Date() + "\n" + Arrays.toString(args), false);
        String hsmyldkLocalPlayVideo = "hsmyldkLocalPlayVideo";
        String s1 = "\"";
        String tips1 = "请在此文件第二行填写视频播放器路径:\n";
        String tips2 = "创建设置文件失败,寄";
        String tips3 = "他奶奶滴,在此文件第二行写视频播放器路径,写路径啊!\n";
        //设置文件
        File setting = new File(rootPath + File.separator + "setting.txt");
        //带着参数进来
        if (args.length > 0 && args[0].toUpperCase().startsWith(hsmyldkLocalPlayVideo.toUpperCase())) {
            //无配置文件
            if (!setting.exists()) {
                if (setting.createNewFile()) {
                    writeText(setting, tips1, true);
                } else {
                    System.out.println(tips2);
                }
            }
            byte[] bytes = new byte[2048];
            FileInputStream fileInputStream = new FileInputStream(setting);
            int read = fileInputStream.read(bytes);
            String s = new String(bytes, 0, read, StandardCharsets.UTF_8);
            String[] split = s.split("\n");
            if (split.length > 1 && split[1].length() > 3) {
                System.out.println(split[1]);
                //使用播放器打开,引号自动补全,防止路径空格
                executeCmd(concatCommand((split[1].startsWith(s1) ? "" : s1) + split[1] + (split[1].endsWith(s1) ? "" : s1), "http" + args[0].substring(hsmyldkLocalPlayVideo.length())));
            } else {
                //有配置文件没写配置
                writeText(setting, tips3, true);
            }
            return;
        }
        //没有参数的情况下启动,首先,写注册表,然后在命令行填写配置文件,反正肯定要手动启动一次
        //创建注册表文件
        String ss = new File(rootPath).getAbsolutePath().replaceAll("\\\\", "\\\\\\\\");
        String s2 = System.getProperty("java.home").replaceAll("\\\\", "\\\\\\\\") + "\\\\bin\\\\java.exe";
        String reg = "Windows Registry Editor Version 5.00\n" + "\n" + "[HKEY_CLASSES_ROOT\\hsmyldkLocalPlayVideo]\n" + "@=\"URL: hsmyldk webVideoPlayer URI\"\n" + "\"URL Protocol\"=\"用来启动本地视频播放器\"\n" + "\n" + "[HKEY_CLASSES_ROOT\\hsmyldkLocalPlayVideo\\DefaultIcon]\n" + "@=\"\\\"" + s2 + "\\\" -classpath " + ss + " hsmyldkVideoPlayerStarter ,1\"\n" + "\n" + "[HKEY_CLASSES_ROOT\\hsmyldkLocalPlayVideo\\Shell]\n" + "\n" + "[HKEY_CLASSES_ROOT\\hsmyldkLocalPlayVideo\\Shell\\Open]\n" + "\n" + "[HKEY_CLASSES_ROOT\\hsmyldkLocalPlayVideo\\Shell\\Open\\command]\n" + "@=\"\\\"" + s2 + "\\\" -classpath " + ss + " hsmyldkVideoPlayerStarter \\\"%1\\\"\"\n";
        File regFile = new File(rootPath + File.separator + "双击打开将class关联进注册表.reg");
        if (!regFile.exists()) {
            if (!regFile.createNewFile()) {
                System.out.println("创建文件失败....,已退出");
                System.exit(0);
            }
        }
        writeText(regFile, reg, false);
        //让用户填写配置
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请将视频播放软件的exe本体拖入此命令行,或输入绝对路径");
            String path = scanner.nextLine();
            File file = new File(path);
            if (file.exists() && file.isFile()) {
                if (!setting.exists()) {
                    if (setting.createNewFile()) {
                        writeText(setting, tips1 + file.getAbsolutePath(), false);
                    } else {
                        System.out.println(tips2);
                    }
                }
                break;
            }
        }
        System.out.println("OK,请运行刚刚生成的注册表文件");
        executeCmd(concatCommand("explorer.exe", "\"" + regFile.getParentFile().getAbsolutePath() + "\""));
    }

    /**
     * 执行cmd命令
     *
     * @param command
     * @throws IOException
     */
    public static String executeCmd(String command) throws IOException {
        return executeCmd(command, false);
    }

    /**
     * 执行cmd命令
     *
     * @param command
     * @throws IOException
     */
    public static String executeCmd(String command, boolean ret) throws IOException {
        //System.out.println(command);
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec("cmd /c " + command);
        if (ret) {
            BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8));
            String line = null;
            StringBuilder build = new StringBuilder();
            while ((line = br.readLine()) != null) {
                build.append(line);
            }
            return build.toString();
        }
        return "";
    }

    public static String concatCommand(String... cmds) {
        StringBuilder sb = new StringBuilder();
        for (String cmd : cmds) {
            sb.append(cmd).append(" ");
        }
        return sb.toString();
    }

    public static void writeText(File file, String text, boolean open) throws IOException {
        FileOutputStream outputStream = new FileOutputStream(file);
        outputStream.write(text.getBytes(StandardCharsets.UTF_8));
        outputStream.flush();
        outputStream.close();
        if (open) {
            executeCmd(concatCommand("notepad.exe", "\"" + file.getAbsolutePath() + "\""));
        }
    }

}
